package cn.nange.cmfz.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.ICaptcha;
import cn.nange.cmfz.entity.User;
import cn.nange.cmfz.service.UserService;
import cn.nange.cmfz.service.UserThreadService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private UserThreadService userThreadService;

    @RequestMapping("login")
    public String login(String name, String pwd, String code, HttpSession session) {

        ICaptcha captcha = (ICaptcha) session.getAttribute("captcha");
        System.out.println("code = " + code);
        System.out.println(captcha.getCode());
        boolean verify = captcha.verify(code);
        if (!verify) {
            return "redirect:/login.jsp";
        } else {
            User login = userService.login(name);
            if (login != null && login.getPwd().equals(pwd)) {
                session.setAttribute("name", name);
                return "redirect:/main.html";
            } else {
                return "redirect:/login.jsp";
            }

        }
    }

    @RequestMapping("captcha")
    public void getCaptcha(HttpServletResponse resp, HttpSession session) throws IOException {
        ICaptcha circleCaptcha = CaptchaUtil.createCircleCaptcha(120, 38, 4, 20);
        session.setAttribute("captcha", circleCaptcha);
        circleCaptcha.write(resp.getOutputStream());
    }

    @RequestMapping("register")
    @ResponseBody
    public Map register(User u) {
        HashMap map = new HashMap();
        try {
            userService.register(u);
            map.put("isAdd", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isAdd", false);
        }
        return map;
    }

    @RequestMapping("ban")
    @ResponseBody
    public Map ban(Integer id) {
        HashMap map = new HashMap();
        try {
            userService.ban(id);
            map.put("isBan", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isBan", false);
        }
        return map;
    }

    @ResponseBody
    @RequestMapping("showOne")
    public User showOne(Integer id) {
        System.out.println(id);
        User user = userService.showOne(id);
        System.out.println("user = " + user);
        return user;
    }

    @ResponseBody
    @RequestMapping("update")
    public Map update(User u) {
        HashMap map = new HashMap();
        try {
            userService.update(u);
            map.put("isUpdate", true);

        } catch (Exception e) {

            e.printStackTrace();

            map.put("isUpdate", false);
        }
        System.out.println("update:" + u);
        return map;
    }

    @ResponseBody
    @RequestMapping("delete")
    public Map delete(int[] id) {
        HashMap map = new HashMap();
        try {
            for (int i = 0; i < id.length; i++) {
                userService.delete(id[i]);
                System.out.println(id[i]);
                map.put("isDelete", true);
            }

        } catch (Exception e) {
            e.printStackTrace();
            map.put("isDelete", false);
        }
        return map;
    }

    @ResponseBody
    @RequestMapping("show")
    public Map show(Integer page, Integer limit) {

        PageInfo<User> show = userService.showAll(page, limit);
        HashMap map = new HashMap();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", show.getTotal());
        map.put("data", show.getList());
        return map;
    }

    @ResponseBody
    @RequestMapping("upload")
    public Map upload(MultipartFile file) throws IOException {
        String s = UUID.randomUUID().toString() + "-" + file.getOriginalFilename();
//        file.transferTo(new File("D://static",s));
        file.transferTo(new File("/Users/nange/static/cmfz", s));
        HashMap map = new HashMap();
        map.put("code", 0);
        map.put("photo", s);
        System.out.println("upload:" + map);
        return map;
    }

    @RequestMapping("sex")
    @ResponseBody
    public List<Map> getSexCount() {
        return userService.showAllBySex();
    }

    @RequestMapping("province")
    @ResponseBody
    public List<Map> getByProvince() {
        return userService.showAllByProvince();
    }

    @RequestMapping("count")
    @ResponseBody
    public Map getByDayCount() {
        return userService.showByDayCount();
    }

    @RequestMapping("counts")
    @ResponseBody
    public Map getByDayt() {
        return userService.showByDay();
    }

    /**
     *  优化
     * @return
     * @throws InterruptedException
     */
    @RequestMapping("thread")
    @ResponseBody
    public Map showAllCountThread() throws Exception {
        long l = System.currentTimeMillis();
        Map map = userService.showByThread();
        long end = System.currentTimeMillis();
        System.out.println(end - l + "ms");

        return map;
    }

    @RequestMapping("threads")
    @ResponseBody
    public Map showAllCount() throws InterruptedException{
        long l = System.currentTimeMillis();
        Map map = userThreadService.showAllCount();
        long end = System.currentTimeMillis();
        System.out.println(end - l + "ms");
        return map;
    }
}
